package com.controller;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.pojo.Permission;
import com.service.IPermissionService;
import com.util.Message;

@Controller
public class PromissionController {

	@Resource
	private IPermissionService permissionService;
	
	//分页
	@RequestMapping(value = "/allPermissionsByPage", method = RequestMethod.POST)
	@ResponseBody
	public Message getpermissions(@RequestBody Message message) {
		List<Permission> list=permissionService.getPermissions(Integer.valueOf(message.getMessage()),message.getGroups());
		//重新获取总数，保证数据事实刷新
		int count = permissionService.getCounts();
		Message mess=new Message();
		mess.setPermission(list);
		mess.setMessage((int)Math.ceil((float)count/message.getGroups())+"");
		return mess;
	}
	
	@RequestMapping(value = "/allPermissions", method = RequestMethod.GET)
	@ResponseBody
	public List<Permission> getallpermissions() {
		List<Permission> list = permissionService.getPermissions();
		return list;
	}

	// 获取总页数
	@RequestMapping(value = "/getPermissionCounts", method = RequestMethod.POST)
	@ResponseBody
	public Message getcounts(@RequestBody Message message) {
		// 获取总数
		int count = permissionService.getCounts();
		Message mess = new Message();
		mess.setMessage((int) Math.ceil((float) count / message.getGroups()) + "");
		return mess;
	}

	@RequestMapping(value = "/deletePermission/{pid}", method = RequestMethod.DELETE)
	@ResponseBody
	public Message deletepermission(@PathVariable int pid) {
		permissionService.deletePermissions(pid);
		permissionService.deletePermission(pid);
		return new Message("success");
	}

	@RequestMapping(value = "/addPermission", method = RequestMethod.POST)
	@ResponseBody
	public Permission addpermission(@RequestBody Permission p) {
		Permission per = permissionService.createPermission(p);
		return per;
	}

	@RequestMapping(value = "/updatePermission", method = RequestMethod.PUT)
	@ResponseBody
	public Permission updatepermission(@RequestBody Permission p) {
		Permission per = permissionService.updatePermission(p);
		return per;
	}
	
	@RequestMapping(value = "/getPermission/{permissionid}", method = RequestMethod.GET)
	@ResponseBody
	public Permission getPermission(@PathVariable int permissionid) {
		Permission p = permissionService.getPermissionByid(permissionid);
		return p;
	}
}
